<template>
  
  <el-button type="info" @click="displayAddDialog">新增</el-button>
  <div v-if="!addDialogDisplay">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed prop="unitCode" label="物料号" width="150" />
      <el-table-column label="用途" width="400">
        <!-- 使用作用域插槽获取当前行的数据 -->
        <template #default="scope">
          <!-- scope.row 就是当前行的完整数据对象 -->
          <!-- 使用 v-for 遍历 scope.row.steps 数组 -->
          <div class="usage" v-for="(usage, index) in scope.row.use" :key="index">
              <span v-for="value in usage.step">{{ value }}-</span>{{ usage.description }}
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleClick">
            修改
          </el-button>
          <el-button link type="primary" size="small" @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div v-else>
    <AppManageUsageTableAdd @submit="submitSuccess" v-show="addDialogDisplay"></AppManageUsageTableAdd>
  </div>
</template>

<script lang="ts" setup>
let data  = await $fetch('/api/usage',{
  method:"get"
})

const tableData = ref(data)

const displayAddDialog = ()=>{
  addDialogDisplay.value = true
}

const submitSuccess = async ()=>{
  addDialogDisplay.value = false
  const res = await $fetch('/api/usage',{
    method:"get"
  })  
  tableData.value = res
  console.log(res)
}

const addDialogDisplay = ref(false)

const handleClick = () => {
  console.log('click')
}

const deleteItem =  async (row)=>{
  console.log(row._id)
  tableData.value = tableData.value?.filter(item=> item._id!=row._id)
  await $fetch(`/api/usage/${row._id}`,{
    method :'delete',
  })
}

</script>

<style lang="css" scoped>
.usage {
  display: flex;
  justify-content: left;
  align-items: center;
  line-height: 30px;
}
</style>